package com.srzk.youthleagueback.service;

import com.srzk.youthleagueback.mapper.RegistrationMapper;
import com.srzk.youthleagueback.pojo.Registration;
import com.srzk.youthleagueback.results.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import static com.srzk.youthleagueback.results.CodeMsg.*;

@Service
public class RegistrationService {
    @Autowired
    private RegistrationMapper mapper;

    public Result<String> addRegistration(Registration registration) {
        try{
            int result = mapper.insert(registration);
            if (result == 1) {
                return Result.success("添加成功");
            } else {
                return Result.error(ADD_FAIL);
            }
        }catch (Exception e){
            int result = mapper.updateByPrimaryKey(registration);
            if (result == 1) {
                return Result.success("更新成功");
            } else {
                return Result.error(UPDATE_FAIL);
            }
        }

    }

    public Result<HashMap<String, String>> getRegistration(String user, String year, int type) {
        Registration result = mapper.selectByPrimaryKey(user, year, type);
        if (result != null) {
            HashMap<String, String> content = new HashMap<String, String>();
            content.put("content", result.getContent());
            return Result.success(content);
        } else {
            return new Result(null, "查找失败");
        }
    }
}
